import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;

public class Test {

    //TODO
    private static String URL = "http://xxxx";
    //TODO
    private static String APP_KEY = "9x8NN55Dppt8VHmfHqZFum7Ke6WJxDOe";
    //TODO
    private static String APP_SECRET = "wIbSUhTIC4nEppYzx2jp9OtPTzLZWnwa";

    public static void main(String[] args) throws Exception {
        //测试
//        bind();
//        register();
//        doubleCall();
//        queryBind();
    }

    private static void bind() throws Exception {
        Map<String, String> params = getCommons();
        params.put("virtNum", "19975540183");
        params.put("phone", "19975619296");
        String sign = createSign(params, APP_SECRET);
        params.put("sign", sign);
        String result = HttpUtil.postForm(URL + "/restful/api/v1/bind", params);
        System.out.println(result);
    }


    private static void register() throws Exception {
        Map<String, String> params = getCommons();
        params.put("virtNum", "19975540183");
        params.put("phone", "19975619296");
        params.put("called", "18768172236");
        String sign = createSign(params, APP_SECRET);
        params.put("sign", sign);
        String result = HttpUtil.postForm(URL + "/restful/api/v1/registerCall", params);
        System.out.println(result);
    }

    public static void doubleCall() throws Exception {
        Map<String, String> params = getCommons();
        params.put("virtNum", "19975540183");
        params.put("called", "18768172236");
        String sign = createSign(params, APP_SECRET);
        params.put("sign", sign);
        String result = HttpUtil.postForm(URL + "/restful/api/v1/doubleCall", params);
        System.out.println(result);
    }

    public static void queryBind() throws Exception {
        Map<String, String> params = getCommons();
        params.put("phone", "19975619296");
        String sign = createSign(params, APP_SECRET);
        params.put("sign", sign);
        String result = HttpUtil.postForm(URL + "/restful/api/v1/queryByPhone", params);
        System.out.println(result);
    }


    private static Map<String, String> getCommons() {
        Map<String, String> params = new TreeMap<>();
        params.put("reqId", uuid());
        params.put("appKey", APP_KEY);

        params.put("timeStamp", DateUtil.dateToString(new Date(), DateUtil.FORMAT_LONG_CLEAN));


        return params;
    }

    public static String uuid() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString().replaceAll("-", "");
    }

    private static String createSign(Map<String, String> parameters, String appSecret) {
        StringBuffer sbkey = new StringBuffer(appSecret);
        parameters.forEach((key, value) -> {
            if (StringUtils.isNotEmpty(value)) {
                sbkey.append(key).append(value.toString());
            }
        });
        sbkey.append(appSecret);
        // MD5加密,结果转换为大写字符
        return DigestUtils.md5Hex(sbkey.toString());
    }

}
